<?php
	if ($login->aid == 0)
		exit;
	$getnotif = $this->get('getnotif');
	$setreadnotif = $this->get('setreadnotif');
	$setnotif = $this->get('setnotif');
    Connection::getConnect();

	if ($getnotif)
	{
    	$sql = "SELECT * FROM notification n WHERE aid=%d LIMIT 1;";
    	Connection::setQueryf($sql, $login->aid);
    	$array = Connection::fetchAll(MYSQL_ASSOC);
    	echo json_encode($array);
		exit;
	}
	if ($setreadnotif)
	{
    	$nid = $this->get('nid');
    	$sql = "DELETE FROM notification WHERE nid=%d;";
    	Connection::setQueryf($sql, $nid);
		exit;
	}
	if ($setnotif)
	{
    	$msg = $this->get('msg');
    	$script = $this->get('script');
    	$sticky = $this->get('sticky');
    	$type = $this->get('type');
    	$all = $this->get('all');
		$action = $this->get('action');
		
		if (strlen($msg) == 0)
			exit;
			
    	$sql = "INSERT INTO notification(msg, script, sticky, type, aid, action) VALUES('%s', '%s', %d, '%s', %d, '%s');";
		if ($all > 0)
		{
			Connection::setQueryf("SELECT aid FROM login WHERE type=%d;", $all);
			$list = Connection::fetchAll();
			foreach ($list as $aid){
    			Connection::setQueryf($sql, $msg, $script, $sticky, $type, $aid[0], $action);
			}
		} else if ($all < 0)
		{
			Connection::setQueryf("SELECT aid FROM login WHERE type>=%d AND type < 100;", $all*-1);
			$list = Connection::fetchAll();
			foreach ($list as $aid){
    			Connection::setQueryf($sql, $msg, $script, $sticky, $type, $aid[0], $action);
			}
		}else {
    		Connection::setQueryf($sql, $msg, $script, $sticky, $type, $login->aid, $action);
		}
		exit;
	}
?>